<html>
<head>
	<title>Gary's Node.js Test Page</title>
	<meta http-equiv="X-UA-Compatible" content="IE=8" />
	<link rel="stylesheet" type="text/css" href='styles.css'/>
	<link rel="stylesheet" type="text/css" href='ext-all.css'/>
	<script src='ext-all.js'></script>
</head>
<body>
</body>
</html>
<script>
    function ajax(config) {
        config = Ext.apply({
            title: "Ajax",
            loadingTarget: false, loadingMsg: true, autoLoading: true, autoStopLoading: true, timeout: 60000,
            defaultSuccessFalseErrorMsg: "Server request unsuccessful",
            success: function (resp, req) {
                var data = null, eMsg;
                try {
                    try {
                        data = Ext.decode(resp.responseText);
                    } catch (e) {
                        data = null;
                    }
                    if (data) {
                        if (data.success) {
                            if (req.successTrue) {
                                req.successTrue(data, req);
                            } // else do nothing
                        } else {
                            if (req.successFalse) {
                                req.successFalse(data, req);
                            }
                        }
                    } else {
                        if (req.dataError) {
                            req.dataError(resp.responseText);
                        } else {
                            showResults("NO Data (and no req.dataError)");
                        }
                    }
                } catch (e) {
                    showResults("ERROR - ajax.success handler");
                }
                try {
                    if (req.cleanup) {
                        req.cleanup(data, req);
                    }
                } catch (e) {
                }
            },
            failure: function (data, req) {
                try {
                    stopLoading();
                    if (req.commError) {
                        req.commError();
                    } else {
                        showResults("ERROR - ajax.failure handler");
                    }
                } catch (e) {
                }
                try {
                    if (req.cleanup) {
                        req.cleanup(data, req);
                    }
                } catch (e) {
                    showResults("ERROR - ajax.cleanup handler");
                }
            }
        }, config);
        return Ext.Ajax.request(config);
    }
    function showResults(txt) {
        Ext.getCmp('serverdata').update(txt);
    }
    function makeViewport() {
        var tbh = 70, 
            tb0 = [
				{ xtype: 'toolbar', height: tbh, docl: 'top',
					items: [
					{ xtype: 'tbtext', text: 'SQL: ' },
					{ xtype: 'textarea', id: 'sqlQuery', height: tbh - 4, rows: 4, cols: 60,
						value: "select top 10 * from pmtrackii..location"
					},
					{ xtype: 'button', text: 'GO',
						handler: function (btn) {
						    var query = Ext.getCmp('sqlQuery').getValue();
						    showResults("sending query: " + query);
						    ajax({
						        url: 'sqlform', params: { sqlQuery: query },
						        successTrue: function (data, req) {
						            showResults(Ext.encode(data));
						        }
						    });
						}
					}
					]
				}
			],
            vpDef = {
                id: 'mainRoot',
                border: false, frame: false,
                margins: 0,
                items: [
                { // The MAIN work area
                    title: 'Node Tester',
                    border: true, frame: false,
                    padding: 10,
                    //dockedItems: tb0,
                    items: [
                    { id: 'endpoint', xtype: 'textfield', width: 300 },
                    { id: 'postdata', xtype: 'textarea', rows: 10, cols: 60 },
                    { xtype: 'button', text: 'POST',
                        handler: function () {
                            var url = Ext.getCmp('endpoint').getValue();
                            var data = Ext.getCmp('postdata').getValue();
                            var json;
                            try {
                                json = Ext.decode(data);
                                showResults("posting to: " + url + "<br/>" + data);
                                ajax({
                                    url: url,
                                    method: 'POST',
                                    params: Ext.encode(json),
                                    successTrue: function (data, req) {
                                        showResults(Ext.encode(data));
                                    }
                                });
                            } catch (e) {
                                showResults("Error in JSON");
                                json = "";
                            }
                        }
                    },
				    { id: 'serverdata', padding: 10, html: 'Results will be displayed here...' }
				    ]
                }
		    ]
            };
        //vpDef = { xtype: 'panel' };
        //Ext.tip.QuickTipManager.init();
        try {
            new Ext.Viewport(vpDef);
        } catch (e) {
            debugger;
        }
    };

    Ext.application({ name: "App", appFolder: 'client/app', launch: makeViewport, autoCreateViewport: false });
</script>